#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Diagnostics;


namespace BESCLR {

	/// <summary>
	/// Summary for ListProcForm
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class ListProcForm : public System::Windows::Forms::Form
	{
	public:
		int SelectedPid;
	public:
		ListProcForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~ListProcForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListView^  ProcListView;
	protected: 
	private: System::Windows::Forms::ColumnHeader^  PIDColumn;
	private: System::Windows::Forms::ColumnHeader^  WinNameColumn;
	private: System::Windows::Forms::ColumnHeader^  PathColumn;
	private: System::Windows::Forms::Button^  LimitButton;
	private: System::Windows::Forms::Button^  RefreshButton;

	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->ProcListView = (gcnew System::Windows::Forms::ListView());
			this->PIDColumn = (gcnew System::Windows::Forms::ColumnHeader());
			this->WinNameColumn = (gcnew System::Windows::Forms::ColumnHeader());
			this->PathColumn = (gcnew System::Windows::Forms::ColumnHeader());
			this->LimitButton = (gcnew System::Windows::Forms::Button());
			this->RefreshButton = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// ProcListView
			// 
			this->ProcListView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^  >(3) {this->PIDColumn, this->WinNameColumn, 
				this->PathColumn});
			this->ProcListView->FullRowSelect = true;
			this->ProcListView->Location = System::Drawing::Point(12, 27);
			this->ProcListView->MultiSelect = false;
			this->ProcListView->Name = L"ProcListView";
			this->ProcListView->Size = System::Drawing::Size(518, 198);
			this->ProcListView->Sorting = System::Windows::Forms::SortOrder::Ascending;
			this->ProcListView->TabIndex = 0;
			this->ProcListView->UseCompatibleStateImageBehavior = false;
			this->ProcListView->View = System::Windows::Forms::View::Details;
			// 
			// PIDColumn
			// 
			this->PIDColumn->Text = L"PID";
			// 
			// WinNameColumn
			// 
			this->WinNameColumn->Text = L"WinName";
			this->WinNameColumn->Width = 129;
			// 
			// PathColumn
			// 
			this->PathColumn->Text = L"Path";
			this->PathColumn->Width = 293;
			// 
			// LimitButton
			// 
			this->LimitButton->Location = System::Drawing::Point(455, 231);
			this->LimitButton->Name = L"LimitButton";
			this->LimitButton->Size = System::Drawing::Size(75, 23);
			this->LimitButton->TabIndex = 1;
			this->LimitButton->Text = L"Limit This";
			this->LimitButton->UseVisualStyleBackColor = true;
			this->LimitButton->Click += gcnew System::EventHandler(this, &ListProcForm::LimitButton_Click);
			// 
			// RefreshButton
			// 
			this->RefreshButton->Location = System::Drawing::Point(361, 231);
			this->RefreshButton->Name = L"RefreshButton";
			this->RefreshButton->Size = System::Drawing::Size(75, 23);
			this->RefreshButton->TabIndex = 2;
			this->RefreshButton->Text = L"Refresh";
			this->RefreshButton->UseVisualStyleBackColor = true;
			this->RefreshButton->Click += gcnew System::EventHandler(this, &ListProcForm::RefreshButton_Click);
			// 
			// ListProcForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(542, 266);
			this->Controls->Add(this->RefreshButton);
			this->Controls->Add(this->LimitButton);
			this->Controls->Add(this->ProcListView);
			this->Name = L"ListProcForm";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
			this->Text = L"ListProcForm";
			this->Load += gcnew System::EventHandler(this, &ListProcForm::ListProcForm_Load);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void ListProcForm_Load(System::Object^  sender, System::EventArgs^  e) {
				 array<Process^>^ processes= Process::GetProcesses();
				 for each (Process^ p in processes)
				 {
					 ListViewItem^ item= gcnew ListViewItem();
					 item->Text=p->Id.ToString();
					 item->Tag=p->Id;
					 item->SubItems->Add(p->MainWindowTitle);
					 item->SubItems->Add(p->ProcessName);
					 ProcListView->Items->Add(item);
				 }
				 SelectedPid=-1;
			 }
private: System::Void LimitButton_Click(System::Object^  sender, System::EventArgs^  e) {
			 if(ProcListView->SelectedItems->Count==0)
			 {
				 MessageBox::Show(this,"Please select a proccess first.");
			 }else{
				 SelectedPid=(int)ProcListView->SelectedItems[0]->Tag;
				 Close();
			 }
		 }
private: System::Void RefreshButton_Click(System::Object^  sender, System::EventArgs^  e) {
			     ProcListView->Items->Clear();
				 array<Process^>^ processes= Process::GetProcesses();
				 for each (Process^ p in processes)
				 {
					 ListViewItem^ item= gcnew ListViewItem();
					 item->Text=p->Id.ToString();
					 item->Tag=p->Id;
					 item->SubItems->Add(p->MainWindowTitle);
					 item->SubItems->Add(p->ProcessName);
					 ProcListView->Items->Add(item);
				 }
		 }
};
}
